package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.SalvageEventsInfo;
import com.sc.nft.entity.vo.AdminSalvageEventsPageVO;
import com.sc.nft.entity.vo.salvageEvents.SalvageEventsPageVO;
import com.sc.nft.enums.NFTProductShowSalvagePrefectureEnum;
import com.sc.nft.mapper.SalvageEventsInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

/**
 * 分解活动主表(sc_salvage_events_info)数据DAO
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-08-10 13:40:39
 */
@Slf4j
@Repository
public class SalvageEventsInfoDao extends ServiceImpl<SalvageEventsInfoMapper, SalvageEventsInfo> {

    public Page<AdminSalvageEventsPageVO> getAdminSalvageEventsPage(String eventsName, Integer pageNo, Integer pageSize) {
        return baseMapper.getAdminSalvageEventsPage(new Page<>(pageNo, pageSize), eventsName);
    }

    public boolean updateIsListingById(Long id, Boolean isListing) {
        LambdaUpdateWrapper<SalvageEventsInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SalvageEventsInfo::getId, id);
        wrapper.set(SalvageEventsInfo::getIsListing, isListing);
        wrapper.set(SalvageEventsInfo::getModifyTime, DateTime.now());
        return update(wrapper);
    }

    public boolean updateSort(Long id, Integer sort) {
        LambdaUpdateWrapper<SalvageEventsInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SalvageEventsInfo::getId, id);
        wrapper.set(SalvageEventsInfo::getSort, sort);
        wrapper.set(SalvageEventsInfo::getModifyTime, DateTime.now());
        return update(wrapper);
    }

    public Page<SalvageEventsPageVO> getSalvageEventsPage(NFTProductShowSalvagePrefectureEnum showSalvagePrefecture, Integer pageNo, Integer pageSize) {
        return baseMapper.getSalvageEventsPage(new Page<>(pageNo, pageSize), ObjectUtil.isNull(showSalvagePrefecture) ? 1 : showSalvagePrefecture.getCode());
    }

    public boolean delById(Long id) {
        LambdaUpdateWrapper<SalvageEventsInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SalvageEventsInfo::getId, id);
        return remove(wrapper);
    }

    public boolean updateInfoById(Long id, String coverImage, String regularImage, Integer sumCount, Integer usableCount, Integer userCount, NFTProductShowSalvagePrefectureEnum showSalvagePrefecture, Boolean consumeConsensus) {
        LambdaUpdateWrapper<SalvageEventsInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SalvageEventsInfo::getId, id);
        wrapper.set(StrUtil.isNotBlank(coverImage), SalvageEventsInfo::getCoverImage, coverImage);
        wrapper.set(StrUtil.isNotBlank(regularImage), SalvageEventsInfo::getRegularImage, regularImage);
        wrapper.set(ObjectUtil.isNotNull(sumCount) && sumCount > 0, SalvageEventsInfo::getSumCount, sumCount);
        wrapper.set(ObjectUtil.isNotNull(usableCount) && usableCount >= 0, SalvageEventsInfo::getUsableCount, usableCount);
        wrapper.set(ObjectUtil.isNotNull(userCount) && userCount >= 0, SalvageEventsInfo::getUserCount, userCount);
        wrapper.set(ObjectUtil.isNotNull(showSalvagePrefecture), SalvageEventsInfo::getShowSalvagePrefecture, showSalvagePrefecture);
        wrapper.set(ObjectUtil.isNotNull(consumeConsensus), SalvageEventsInfo::getConsumeConsensus, consumeConsensus);
        return update(wrapper);
    }

    public boolean subUsableCountById(Long id, Integer sumCount) {
        return baseMapper.subUsableCountById(id, sumCount);
    }

    public SalvageEventsInfo getLastOneByShowSalvagePrefecture(NFTProductShowSalvagePrefectureEnum type) {
        LambdaQueryWrapper<SalvageEventsInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SalvageEventsInfo::getShowSalvagePrefecture, type);
        wrapper.orderByDesc();
        wrapper.last("limit 1");
        return getOne(wrapper);
    }
}